<?php

/**
 * Template to render div with one question
 * It will be complete with
 * up/down vote data and links
 * IsAcceptedAnswer check mark,
 *
 * Question body, tags, author block
 * datetime
 *
 *  IMPORTANT: this template will accept array
 *  straight from QUESTIONS collection
 *
 */
class tplQuestion extends StudyBuddy\Template\Template {

    protected static function func(&$a) {
        if (array_key_exists('a_edited', $a)) {
            /**
             * A way to pass "translated"
             * version of "Edited" word
             * to the tplEditedby template
             */
            $aEdited = end($a['a_edited']);
            $aEdited['edited'] = $a['edited'];

            $a['edits'] = \tplEditedby::parse($aEdited, false);
        }

        if (!empty($a['i_sticky'])) {
            $a['sticky'] = ' sticky';
        }

        if (!empty($a['a_comments'])) {
            /**
             * Closure function
             * to pass resource_id 
             * and author id of this
             * Question to the tplComments
             * This way we don't have to store
             * duplicate data in each comment
             * element and still be able to
             * have access to these 2 important
             * fields in the tplComments template
             * We going to need id or resource owner
             * in order to add it to the "reply" link
             * in the form of class uid-$uid
             * 
             */
            $rid = $a['_id'];
            $uid = $a['i_uid'];
            $reply = $a['reply'];
            $reply_t = $a['reply_t'];

            $f = function(&$data) use ($rid, $uid, $reply, $reply_t) {
                        $data['resource_id'] = $rid;
                        $data['owner_id'] = $uid;
                        $data['reply'] = $reply;
                        $data['reply_t'] = $reply_t;
                    };

            $a['comments_html'] = tplComment::loop($a['a_comments'], true, $f); //
        }
        if(!empty($a['hts'])) {
            $a['hts_ago'] = \StudyBuddy\TimeAgo::format(new \DateTime($a['hts']));
        }
        if(!empty($a['tags_html'])) {
            $a['custom_tags_html'] = str_replace('%1$s', 'quest-tag', $a['tags_html']);
        }
		if (empty($a['dif'])) {
            $a['dif'] = '1';
        }
    }

    protected static $vars = array(
        '_id' => '', // 1
        'b' => '', // 2
        'ulink' => '', // 3
        'avtr' => '', // 4
        'tags_html' => '', // 5
        'credits' => '', // 6
        'hts' => '', // 7
        'i_votes' => '', // 8
        'i_favs' => '', // 9
        'i_uid' => '0', // 10 Question author id
        'i_views' => '0', // 11
        'vw_s' => 's', // 12
        'vote_up' => "\xE2\x87\xA7", // 13 \xE2\x87\xA7
        'vote_down' => "\xE2\x87\xA9", // 14
        'i_flags' => '', //15
        'deleted' => '', // 16
        'deletedby' => '', //17
        'edits' => '', //18
        'sticky' => '', //19
        'comments_html' => '', //20
        'i_comments' => '0', //21
        'nocomments' => '', //22
        'add_comment' => 'add comment', //23
        'dif' => '1', //24
        'hts_ago' => '', //25
        'custom_tags_html' => '', //26
        'i_ans' => '', //27
        'ans_s' => '', //28
        'followhtml' => '', //29
    );
    protected static $tpl = '
	<div class="quest-left">
		<a href="JavaScript:void(0)"><img src="%4$s" height="30" width="30" alt=""></a>
		<a href="JavaScript:void(0)" id="score%1$s" class="qscore boxl-tdn" style="width:30px; text-decoration:none; color:#fff; display:none;">%8$s</a>
		%29$s
		<a href="JavaScript:void(0)" style="margin-top:5px;"><img src="/images/difico/%24$s.png" width="25px" height="20px" /></a>
	</div>
	<div class="quest-right">
		<div class="question-body" id="qbody-%1$s">%2$s</div>
		<div class="quest-rm">
			%26$s
		</div>
		<div class="quest-rb">
			asked by %3$s %25$s | <span class="qa_abuse qabuse"><span id="res_%1$s" class="abuse ajax" style="cursor:pointer">Report abuse</span></span>
		</div>
		<div class="question%19$s controls%16$s uid-%10$s cb" id="res_%1$s">
			<span class="icoc stub fr">&nbsp;</span>
		</div>
	</div>
	<div class="quest-nav">
		<div class="quest-nl">
			<div class="quest-nlt">%27$s <span>answer%28$s</span></div>
			<div class="quest-nlt">%11$s <span>view%12$s</span></div>
		</div>
		<div class="quest-nr">
			<div class="quest-nr-txt">
				Filter answers by :
			</div>    
			<div class="quest-nr-btn">
				<input id="i_votes" type="button" class="ajax sortans qtype oldest-btn" value="" style="border:none" />
			</div>
			<div class="quest-nr-btn">
				<input id="i_ts" type="button" class="ajax sortans qtype votes-btn" value="" style="border:none" />
			</div>
		</div>
	</div>';

}
